<?php

namespace post\modules\post\controllers;


use PHPUnit\Framework\Exception;
use post\modules\post\models\Comment;
use post\modules\post\models\Post;
use post\modules\post\models\Zan;
use post\modules\user\models\User;
use post\modules\user\models\UserInfo;
use Yii;

use yii\data\Pagination;
use yii\web\Controller;
use post\base\BaseController;

class PostShowController extends BaseController
{


    public function actionShow()
    {
        $redis = \Yii::$app->redis;
        $request = Yii::$app->request;
        $id = $request->get('id');
        $post = Post::find()->where(['id' => $id])->asArray()->all();

        $comments = Comment::find()->where(['post_id' => $id])->asArray()->all();
        $arr = [];
        $arr['post'] = $post;
        $arr['comments'] = $comments;
        return $this->json_ok($arr);
    }

    public function actionComments()
    {
        $redis = \Yii::$app->redis;
        $request = Yii::$app->request;
        $id = $request->get('id');
        if ($redis->get('j_post-show' . $id) == null) {
            $comments = Comment::find()->where(['post_id' => $id])->asArray()->all();
            foreach ($comments as &$item) {
                $id = $item['user_id'];
                $username = User::find()->where(['id' => $id])->select(['username'])
                    ->asArray()->one();
                $tou = UserInfo::find()->where(['user_id' => $id])->select(['tou'])
                    ->asArray()->one();
                $item['username'] = $username['username'];
                $item['tou'] = $tou['tou'];

            }
            $data = $this->json_ok($comments);
            $redis->setex('j_post-show' . $id, 864000, $data);
            return $this->json_ok($comments);
        } else {
            $post = Yii::$app->redis->get('j_post-show' . $id);

            return $post;

        }


    }

    public function actionIndex()
    {
        $request = \Yii::$app->request;
        $page = $request->get("page");
        if ($page == null) {
            $page = 1;
        }


        $redis = \Yii::$app->redis;

        try {

            if ($redis->get('j_post' . $page) == null) {

//                echo "没有!";
                $sizeCount = 0;   //一共分多少页
                //每页的数量*(浏览页-1)+1
                $pageSize = Yii::$app->params['pageSize'];   //每页的个数从配置文件读取
                $paindex = $pageSize * ($page - 1);

                $query = Post::find();
                $count = $query->count();


                if ($count % $pageSize != 0) {
                    $sizeCount = intval($count / $pageSize) + 1;
                } else {
                    $sizeCount = $count / $pageSize;
                }

                $articles = Post::find()
                    ->where(['isok' => 1])
                    ->limit($pageSize)
                    ->offset($paindex)
                    ->orderBy('created_at desc')
                    ->all();


                foreach ($articles as &$item) {
                    $comments = $item->comments;
                    $zans = $item->zans;
                    $user = $item->user;
                    $item = $item->toArray();
                    $item['comments'] = $comments;
                    $item['zans'] = $zans;
                    $item['user'] = $user;

                }
                $data[] = null;
                $data["data"] = $articles;
                $data["sizeCount"] = $sizeCount;
                $data["page"] = $page;

                $data = $this->json_ok($data);

                //设置10天过期的
                Yii::$app->redis->setex('j_post' . $page, 864000, $data);
                $posts = Yii::$app->redis->get('j_post' . $page);
                return $posts;

            } else {

                $posts = Yii::$app->redis->get('j_post' . $page);


                return $posts;

            }
        } catch (Exception $exception) {
            echo "Redis 连接失败了!";
            var_dump($exception);
        }


    }


    public function actionDelete()
    {
        Yii::$app->redis->flushall();
    }

    public function actionAll()
    {
        $posts = Yii::$app->redis->keys("*");

        dd($posts);
    }

    public function actionDd()
    {
        $key = Yii::$app->request->get('key');
        Yii::$app->redis->del($key);
    }

}
